﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ControlServer.Controllers {
    [HandleError]
    public class DeviceController : Controller {
        public ActionResult Index() {
            //ControlServer.MvcApplication.ControlMain.
            //Dictionary<string, ControlPoint.ControlAPI.Device> devices; 
            //= new Dictionary<string,ControlPoint.ControlAPI.Device>();
            /*ControlServer.MvcApplication.ControlMain.De
            foreach (ControlPoint.ControlAPI.Device device in ControlServer.MvcApplication.ControlMain.Devices.Values) {
                devices.Add(device.ID, device.Name);
            }*/
            ViewData["Devices"] = ControlServer.MvcApplication.ControlMain.Devices.Values.ToList<ControlPoint.ControlAPI.Device>();
            ViewData["Message"] = "Welcome to ASP.NET MVC!";

            return View();
        }


        public ActionResult Settings(string id) {
            if (id != null) {
                id = ControlServer.Utilities.base64Decode(HttpUtility.UrlDecode(id));
            } else {
                id = "nothing";
            }
            Console.WriteLine("Test");
            //ViewData["id"] = HttpUtility.
            ViewData["name"] = id;
            ViewData["device"] = ControlServer.MvcApplication.ControlMain.Devices[id];
            Dictionary<String, Object> settings = new Dictionary<String, Object>(); 
            foreach (ControlPoint.ControlAPI.Setting setting in ControlServer.MvcApplication.ControlMain.Devices[id].Settings.Settings.Values) { 
                settings.Add(setting.Name,  setting.Value);
            }
            ViewData["settings"] = settings;
             
            return View();
        }

		public ActionResult Add() {
            Dictionary<String, String> _devices = new Dictionary<String, String>();

            foreach (ControlPoint.ControlAPI.DeviceController dc in ControlServer.MvcApplication.ControlMain.Controllers.Values) {
                foreach (var k in dc.AvailableDevices) {
                    _devices.Add(k.Key, k.Value);
                }
            }
			
            return View(_devices);
		}
        public ActionResult Add2() {
            Dictionary<String, String> _devices = new Dictionary<String, String>();

            foreach (ControlPoint.ControlAPI.DeviceController dc in ControlServer.MvcApplication.ControlMain.Controllers.Values) {
                foreach (var k in dc.AvailableDevices) {
                    _devices.Add(k.Key, k.Value);
                }
            }
            ViewData["Devices"] = _devices;

            return View();
        }
		
        //
        // GET: /Device/Create

        public ActionResult Off() {
            ((ControlPoint.ControlAPI.ZWaveDimmer)ControlServer.MvcApplication.ControlMain.Devices["zw:21815248:2:38:0"]).Off();
            return View();
        }
        //
        // GET: /Device/Create

        public ActionResult On() {
            ((ControlPoint.ControlAPI.ZWaveDimmer)ControlServer.MvcApplication.ControlMain.Devices["zw:21815248:2:38:0"]).Dim(50);
            return View();
        } 


        public ActionResult About() {
            return View();
        }
    }
}



